package org.lgiavedoni.android.franq.command.system;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.lgiavedoni.android.franq.core.command.Command;
import org.lgiavedoni.android.franq.core.speak.CommonPhraseHelper;
import org.lgiavedoni.android.franq.service.communication.CommunicationService;
import org.lgiavedoni.android.franq.util.collection.CollectionUtils;
import org.lgiavedoni.android.franq.util.phone.wifi.WifiManager;

import com.google.inject.Inject;

public class WifiCommand extends Command {

	private final WifiManager wifiManager;

	private static final String OPTION_ON = "on";
	private static final String OPTION_OFF = "off";
	private static final String OPTION_LIST = "list";

	@Inject
	public WifiCommand(WifiManager wifiManager) {
		super();
		this.wifiManager = wifiManager;
	}

	@Override
	public void setOptions(Options options) {
		options.addOption(OPTION_ON, false, "Enable WiFi");
		options.addOption(OPTION_OFF, false, "Disable WiFi");
		options.addOption(OPTION_LIST, false, "List WiFi Networks");
	}

	@Override
	public boolean execute(CommandLine cmd) throws Exception {
		if (cmd.hasOption(OPTION_LIST)) {
			CommunicationService.inst.sendDefaultTextMessage("Wifi BSSID: " + CollectionUtils.toString(wifiManager.listWifiNetworksBSSID().toArray(), " - "));
			return true;
		}

		if (cmd.hasOption(OPTION_ON)) {
			wifiManager.turnON();
			CommunicationService.inst.sendMessage(CommonPhraseHelper.DONE);
			return true;
		}

		if (cmd.hasOption(OPTION_OFF)) {
			wifiManager.turnOFF();
			CommunicationService.inst.sendMessage(CommonPhraseHelper.DONE);
			return true;
		}
		return false;
	}

	@Override
	public String getCommandText() {
		return "wifi";
	}
}